/**
 * DSM - Distributed Search Manager
 * Developed by Milspec Research International Pty Ltd
 * $Author: bslattery $
 * $Revision: 1.4 $
 * $Date: 2007/08/22 05:15:50 $
 * (c)Copyright 2004
 * education.au limited
 * DEST
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the names education.au limited, DEST nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * To the extent permitted by law, the copyright owners of this software and
 * its contributors:
 * (i) exclude all warranties in relation to the software; and
 * (ii) exclude liability for all losses, costs, expenses and damages arising
 * in any way from the use of the software whether arising from or in
 * relation to breach of contract, negligence or any other tort, in equity
 * or otherwise. If the software is in breach of a warranty which is
 * implied by law, the copyright owners and contributors liability is
 * limited to the replacement of the software.
 *
 * @author	gsingh
 */

package au.edu.educationau.opensource.dsm.adapters;

import java.io.Reader;
import java.util.ArrayList;

import au.edu.educationau.opensource.dsm.DeusExMachina;
import au.edu.educationau.opensource.dsm.adapters.contenthandler.SolrContentHandler;
import au.edu.educationau.opensource.dsm.adapters.querygenerator.QueryGenerator;
import au.edu.educationau.opensource.dsm.adapters.querygenerator.SolrQueryGenerator;
import au.edu.educationau.opensource.dsm.cache.DiskCacheEntry;
import au.edu.educationau.opensource.dsm.obj.SearchCriteria;
import au.edu.educationau.opensource.dsm.service.PropertiesService;
import au.edu.educationau.opensource.dsm.util.Flog;
import au.edu.educationau.opensource.dsm.util.StringUtils;
import au.edu.educationau.opensource.dsm.worker.ResultSetUnifier;

/**
 * Solr search engine searching. Parses the standard Solr XML result to extract
 * result info.
 */

public class SolrSearchAdapterImpl extends SearchAdapter {

	/** Parent constructor */
	public SolrSearchAdapterImpl() {
		super();
	}

	/**
	 * See the SearchAdapter abstract method doPrime
	 * 
	 * @exception Throwable
	 */
	public void doPrime() throws Throwable {
		SearchCriteria criteria = getSearchCriteria();
		// addToURLBuffer("qt=dismax&start=0");
		// addToURLBuffer("qt=standard&start=0");
		addToURLBuffer("start=0");
		if (super.getProperties().getBatchSize() != 0 && StringUtils.isEmpty(criteria.getCustomParamValue(getProperties().getAdapterCode() + ".rows"))) {
			addToURLBuffer("&rows=" + getProperties().getBatchSize());
		}
		addToURLBuffer(generateSearchQuery(criteria, getProperties()));
	}

	private String generateSearchQuery(SearchCriteria criteria, SearchAdapterProperties props) {
		
		
		String queryGenerator = PropertiesService.getStringProperty(props.getAdapterCode() + ".query.generator", SolrQueryGenerator.class.getName());
		try {			
			Class generatorClass = Class.forName(queryGenerator);
			QueryGenerator generator = (QueryGenerator) generatorClass.newInstance();
			return generator.generateQueryParams(criteria, props);
		} catch (Exception e) {
			Flog.error(toString(), "error generating query string", e);
			return "";
		}
	}

	/**
	 * See the SearchAdapter abstract method doPerform
	 * 
	 * @exception Throwable
	 */
	public ArrayList doPerform() throws Throwable {
		SolrContentHandler cHandler = new SolrContentHandler();
		cHandler.setWebsiteURL(super.getProperties().getWebsiteURL());
		cHandler.setSearchAdapterProperties(super.getProperties());
		cHandler.setSearchCriteria(super.getSearchCriteria());
		DiskCacheEntry entry = DeusExMachina._DiskCache().getEntry(super.getAugmentedSearchURL());
		if (null != entry) {
			Reader reader = entry.getBufferedReader();
			if (null != reader) {
				cHandler = (SolrContentHandler) ResultSetUnifier.processXML(reader, cHandler, "UTF-8");
			}
		}
		ArrayList batch = cHandler.getResults();
		super.setTheoreticalAvailableResults(cHandler.getFound());
		super.setFacetQueryCounts(cHandler.getFacetQueryCounts());
		return batch;
	}

	/**
	 * See the SearchAdapter abstract method doNextBatch
	 * 
	 * @exception Throwable
	 */
	public boolean doNextBatch() throws Throwable {
		return false;
	}

	/**
	 * See the SearchAdapter abstract method doStopNice
	 * 
	 * @exception Throwable
	 */
	public void doStopNice() throws Throwable {
		// does nothing here
	}

	/**
	 * See the SearchAdapter abstract method doStopForce
	 * 
	 * @exception Throwable
	 */
	public void doStopForce() throws Throwable {
		// does nothing here
	}

	/** Class display string */
	public String toString() {
		return "o.m.d.a.SolrSearchAdapterImpl";
	}

} // -- EOF
